package com.xinzhi.savingpot.task.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinzhi.savingpot.enums.StatusCode;
import com.xinzhi.savingpot.rminfo.mapper.IEarnestmoneyMapper;
import com.xinzhi.savingpot.task.entity.TaskInfo;
import com.xinzhi.savingpot.task.entity.TaskStateInfo;
import com.xinzhi.savingpot.task.entity.TaskSteps;
import com.xinzhi.savingpot.task.mapper.TaskInfoMapper;
import com.xinzhi.savingpot.task.vo.TaskImg;
import com.xinzhi.savingpot.task.vo.TaskInfoVo;
import com.xinzhi.savingpot.task.service.ITaskinfoService;
import com.xinzhi.savingpot.task.vo.TaskListVo;
import com.xinzhi.savingpot.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-01-05
 */
@Service
public class TaskinfoServiceImpl implements ITaskinfoService {

    @Autowired
    private TaskInfoMapper taskinfoMapper;
    @Autowired
    private IEarnestmoneyMapper iEarnestmoneyMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ImageUtil imageUtil;
    @Autowired
    private IDUtils idUtils;

    @Override
    public BaseResponse addTask(TaskInfoVo tiv, Integer userID) {
        BigDecimal earnestmoney = iEarnestmoneyMapper.getEarnestmoney(userID);
        if (earnestmoney.compareTo(new BigDecimal(500)) == -1) return new BaseResponse(400, "保障金余额不足，请及时充值");
        String s = idUtils.genImageName();
        TaskInfo ti = new TaskInfo();
        TaskSteps tss ;
        ti.setTaskNumber(s);
        ti.getUserInfo().setUserId(userID);
        ti.getTaskClass().setTaskClassId(tiv.getTaskCLassVo().getTaskClassId());
        ti.setTaskTitle(tiv.getTaskTitle());
        ti.setFrequency(tiv.getFrequency());
        ti.setDec(tiv.getDec());
        ti.setTaskUnitPrice(tiv.getTaskUnitPrice());
        ti.setSumPlaces(tiv.getSumPlaces());
        ti.setRemainPlaces(tiv.getRemainPlaces());
        ti.setReleaseTime(new Date());
        ti.setEndTime(tiv.getEndTime());
        ti.setNumLook(0);
        ti.setTrust(0);
        ti.setTaskSoftware("test");

        for (TaskImg tim: tiv.getTaskImg()) {
            tss = new TaskSteps();
            tss.setTaskSort(tim.getImgNumber());
            tss.setTaskStepIntro(tim.getStepExplain());
            String imgPath = "";
            try {
                imgPath = new ObjectMapper().writeValueAsString(FtpUtil.uploadPicture(tim.getImgFile()));
                tss.setTaskStepUrl(imgPath);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            ti.getTasksteps().add(tss);
        }
//        任务总价
        ti.setAmountAdvanced(tiv.getTaskUnitPrice().multiply(new BigDecimal(tiv.getSumPlaces())));
        int i = taskinfoMapper.addTask(ti);
        if (i >0){
            redisUtil.hset("check_task:"+userID,s,ti,1000);
            return new BaseResponse(200, "任务发布成功，进入待审核状态");
        } else {
            return new BaseResponse(400, "任务发布失败，请重试");
        }

//        Map<Object, Object> hmget = redisUtil.hmget("check_task:" + tiv.getUserid());

    }

//    重新提供一个VO
    @Override
    public BaseResponse queryTaskInfo(Integer taskId) throws IOException {
        if (!redisUtil.hasKey("task"+taskId)) {
            TaskInfo ti;
            ti = taskinfoMapper.queryTaskById(taskId);
            if (ti != null) {
                TaskInfoVo tiv = new TaskInfoVo();
                tiv.setTaskId(ti.getTaskId());
                tiv.setTaskTitle(ti.getTaskTitle());
                tiv.setTaskNumber(ti.getTaskNumber());
                System.out.println(ti.getTaskClass().getTaskClassName());
                tiv.getTaskCLassVo().setTaskClassName(ti.getTaskClass().getTaskClassName());
                tiv.setFrequency(ti.getFrequency());
                tiv.setDec(ti.getDec());
                tiv.setTaskUnitPrice(ti.getTaskUnitPrice());
                tiv.setRemainPlaces(ti.getRemainPlaces());
                tiv.setSumPlaces(ti.getSumPlaces());
                tiv.setNumLook(ti.getNumLook());
                tiv.setTrust(ti.getTrust());
                tiv.getUiv().setUserID(ti.getUserInfo().getUserId());
                tiv.getUiv().setUserNumber(ti.getUserInfo().getUserNumber());
                tiv.getUiv().setUsernickname(ti.getUserInfo().getUserNickName());
                tiv.getUiv().setUserheadphoto(imageUtil.image2Base64(ti.getUserInfo().getUserHeadPhoto()));

                redisUtil.set("task"+taskId, ti, 600);
                return new BaseResponse(1, "获取成功", tiv);
            } else {
                return new BaseResponse(1, "获取失败", null);
            }
        }
        TaskInfo ti = (TaskInfo) redisUtil.get("task" + taskId);
        return new BaseResponse(1, "获取成功", ti);
    }

}
